package socket

import (
	"fmt"
	"net"
)

func UDPClient() {
	con, err := net.DialUDP("udp", nil, &net.UDPAddr{
		IP:   net.ParseIP("127.0.0.1"),
		Port: 9001,
	})

	if err != nil {
		fmt.Println("udp client start failed, err=" + err.Error())
		return
	}

	fmt.Println("udp client started")

	defer con.Close()

	for i := 0; i < 10; i++ {
		con.Write([]byte("hello world"))
	}

	var data [1024]byte
	for {
		n, remoteAddr, err := con.ReadFromUDP(data[:])
		if err != nil {
			fmt.Println("err=" + err.Error())
			return
		}
		if n == 0 {
			fmt.Println("not receive")
		}

		msg := string(data[:n])
		fmt.Println(msg, *remoteAddr)
	}
}
